package com.ostrich.module_main.ui

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.ostrich.library_base.BaseActivity
import com.ostrich.library_common.utils.LoggerUtils
import com.ostrich.module_main.R
import com.ostrich.module_main.bean.CustomBean
import com.ostrich.module_main.databinding.MainActivityGuideBinding
import com.ostrich.module_main.viewholder.CustomPagerViewHolder
import com.tencent.mmkv.MMKV
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.adapter.OnPageChangeListenerAdapter
import com.zhpan.bannerview.constants.TransformerStyle
import com.zhpan.bannerview.holder.HolderCreator
import com.zhpan.bannerview.utils.BannerUtils
import com.zhpan.indicator.enums.IndicatorSlideMode
import java.util.*


class GuideActivity : BaseActivity<MainActivityGuideBinding>(), HolderCreator<CustomPagerViewHolder> {
    private lateinit var viewPager:BannerViewPager<CustomBean,CustomPagerViewHolder>
    //动画效果
    private val transforms = intArrayOf(TransformerStyle.NONE, TransformerStyle.ACCORDION, TransformerStyle.STACK, TransformerStyle.DEPTH, TransformerStyle.ROTATE, TransformerStyle.SCALE_IN)
    //描述
    private val des = arrayOf("在这里\n你可以听到周围人的心声", "在这里\nTA会在下一秒遇见你", "在这里\n不再错过可以改变你一生的人")
    //图片
    private val mDrawableList: MutableList<Int> = ArrayList()
    //数据源
    private val data:MutableList<CustomBean> = mutableListOf()
    //准备数据源
    init {
        mDrawableList.add(R.drawable.guide0)
        mDrawableList.add(R.drawable.guide1)
        mDrawableList.add(R.drawable.guide2)
        for(i in 0 ..2){
            val customBean = CustomBean()
            customBean.imageRes = mDrawableList[i]
            customBean.imageDescription = des[i]
            data.add(customBean)
        }
    }

    override fun initView() {
        //状态栏
        ImmersionBar.with(this)
            .statusBarView(binding?.topView)
            .init()


        viewPager = binding?.viewpager as BannerViewPager<CustomBean,CustomPagerViewHolder>
        viewPager.setAutoPlay(false)//是否自动播放
            .setCanLoop(false)//是否循环
            .setPageTransformerStyle(transforms[Random().nextInt(6)])//动画效果，6个动画随机取
            .setScrollDuration(ANIMATION_DURATION)//翻页时常
            .setIndicatorMargin(0, 0, 0, resources.getDimension(com.ostrich.library_common.R.dimen.dp_100).toInt())
            .setIndicatorGap(resources.getDimension(com.ostrich.library_common.R.dimen.dp_10).toInt())
            .setIndicatorColor(ContextCompat.getColor(this, R.color.white),
                ContextCompat.getColor(this, R.color.white_alpha_75))
            .setIndicatorSlideMode(com.zhpan.bannerview.constants.IndicatorSlideMode.WORM)
            .setIndicatorRadius(resources.getDimension(com.ostrich.library_common.R.dimen.dp_3).toInt(), resources.getDimension(com.ostrich.library_common.R.dimen.dp_3).toInt())
            .setOnPageChangeListener(object : OnPageChangeListenerAdapter() {
                override fun onPageSelected(position: Int) {
                    LoggerUtils.d("main","引导页 $position")
                    updateUI(position)
                }
            })
            .setOnPageClickListener {
                ToastUtils.showShort("点击了 $it")
            }
            .setHolderCreator(this) //设置每个item的viewHolder
            .create(data)//设置数据源

    }

    /**
     * 翻页更新UI
     */
    private fun updateUI(position: Int) {
        binding?.let {
            it.tvDescribe.text = des[position]
            //文字属性动画
            val alpha = ObjectAnimator.ofFloat(it.tvDescribe, "alpha", 0f, 1f)
            val translationX = ObjectAnimator.ofFloat(it.tvDescribe, "translationX", -120f, 0f)
            val animatorSet = AnimatorSet()
            animatorSet.playTogether(
                alpha,
                translationX
            )
            animatorSet.duration = ANIMATION_DURATION.toLong()
            animatorSet.start()
            //是否显示startNow
            if(position == viewPager.list.size - 1 && it.btnStart.visibility == View.GONE){
                it.btnStart.visibility = View.VISIBLE
                val alpha = ObjectAnimator.ofFloat(it.btnStart, "alpha", 0f, 1f)
                alpha.duration = ANIMATION_DURATION.toLong()
                alpha.start()
            }else{
                it.btnStart.visibility = View.GONE
            }


        }

    }

    override fun initData() {

    }

    override fun initListener() {
        binding?.let {
            it.btnStart.setOnClickListener {
                startActivity(Intent(this,MainActivity::class.java))
                finish()
            }
        }

    }

    override fun bindLayout(): Int {
        return R.layout.main_activity_guide
    }

    companion object{
        private const val ANIMATION_DURATION = 1300
    }

    override fun createViewHolder(): CustomPagerViewHolder {
        return CustomPagerViewHolder()
    }
}